function [ results ] = Return_EHT_Regressions( data, REC, h, beg, fin, min_mat, max_mat, runOoS, CPrt, R)

%%%%%%%%%%%%%%%%%%%%%%%%PREPARE VARIABLES%%%%%%%%%%%%%%%

v_mat=data(1,2:end)/12; %all maturities
%yh=log(1+data(1+beg:end-fin,find(data(1,:)==h))/100);
%m_y=log(1+data(1+beg:end-fin,2:end)/100);
yh=data(1+beg:end-fin,find(data(1,:)==h));
m_y=data(1+beg:end-fin,2:end);

T=size(m_y,1);
N=size(m_y,2)-1;

%h-month forward spreads, term spreads, and h-month returns
m_f=[ones(T,1)*v_mat(2:end)].*m_y(:,2:end)-(ones(T,1)*v_mat(1:end-1)).*m_y(:,1:end-1);
m_fs=m_f-yh*(h/12)*ones(1,N);
m_ys=m_y(:,2:end)-yh*ones(1,N);
m_r=[ones(T-h,1)*v_mat(2:end)].*m_y(1:end-h,2:end)-(ones(T-h,1)*v_mat(1:end-1)).*m_y(1+h:end,1:end-1);
m_rx=[NaN(h,N); m_r-yh(1:end-h,1)*(h/12)*ones(1,N)];

%estimate CP factor original way with 1-year forward rates and returns

%first compute 1-year fwds and returns
[CP_mat CP_idx]=find(ones(5,1)*v_mat(1:end)==[1:5]'*ones(1,N+1));
m_f_CP=[ones(T,1)*(2:5)].*m_y(:,CP_idx(2:end))-(ones(T,1)*(1:4)).*m_y(:,CP_idx(1:end-1));
m_r_CP=[ones(T-12,1)*(2:5)].*m_y(1:end-12,CP_idx(2:end))-(ones(T-12,1)*(1:4)).*m_y(1+12:end,CP_idx(1:end-1));
m_rx_CP=[NaN(12,4); m_r_CP-m_y(1:end-12,CP_idx(1))*ones(1,4)];

%now run full-sample regression
Y=mean(m_rx_CP(1+12:end,:),2);
X=[ones(T-12,1) m_y(1:end-12,CP_idx(1)) m_f_CP(1:end-12,:)];
[gamma]=olsgmm(Y,X,12,1);
CP=[ones(T,1) m_y(1:end,CP_idx(1)) m_f_CP(1:end,:)]*gamma;

if CPrt==1; %real time CP
    CPrt=NaN(T,1);
    for t=R:T;
        Y=mean(m_rx_CP(1+12:t,:),2);
        X=[ones(t-12,1) m_y(1:t-12,CP_idx(1)) m_f_CP(1:t-12,:)];
        [gamma]=olsgmm(Y,X,12,1);
        if t==R; 
            CPrt(1:t)=[ones(t,1) m_y(1:t,CP_idx(1)) m_f_CP(1:t,:)]*gamma;
        else
            CPrt(t)=[1  m_y(t,CP_idx(1)) m_f_CP(t,:)]*gamma;
        end;
    end;
    CP=CPrt;
end;

%find min and max maturity for plots
[c_idx min_max_idx]=find(ones(2,1)*v_mat(1:end)==[min_mat max_mat]'*ones(1,N));
v_idx=(min_max_idx(1):min_max_idx(2));
N1=size(v_idx,2);

%%%%%%%%%%%%%PREDICTABILITY REGRESSIONS%%%%%%%%%%%%%%%%%%%%%%%%%

v_beta=NaN(N1,2,3);
v_beta_rec=NaN(N1,2,3);
v_beta_exp=NaN(N1,2,3);
v_beta_sign=NaN(N1,3);
v_beta_rec_sign=NaN(N1,3);
v_beta_exp_sign=NaN(N1,3);
v_r2=NaN(N1,2,3);
v_r2_rec=NaN(N1,2,3);
v_r2_exp=NaN(N1,2,3);
v_OoSr2=NaN(N1,2,3);
v_OoSr2_rec=NaN(N1,2,3);
v_OoSr2_exp=NaN(N1,2,3);

for j=1:3;
    
    for i=1:N1;
        
        current_idx=v_idx(i);
        
        %find first observation for current maturity
        sumnans = sum(isnan(m_fs(:,current_idx)));
        cumsumnans = cumsum(isnan(m_fs(:,current_idx)));
        firstobs = find((sumnans - [0; cumsumnans(1:end-1)])==0,1,'first');
        
        Y=m_rx(firstobs+h:end,current_idx);
        
        if j==1;
            X=[ones(T-h-firstobs+1,1) m_ys(firstobs:end-h,current_idx)];
        elseif j==2;
            X=[ones(T-h-firstobs+1,1) m_fs(firstobs:end-h,current_idx)];
        elseif j==3;
            X=[ones(T-h-firstobs+1,1) CP(firstobs:end-h,1)];
        end;
        
        RECh=REC(firstobs:end-h,1);
        index_RECh=find(RECh==1);
        index_EXPh=find(1-RECh==1);
        
        [bv,sebv,R2v,R2vadj,v,F]=olsgmm(Y,X,h,1);
        
        e_HA=(Y-mean(Y));
        e=(Y-X*bv);
        v_beta(i,:,j)=[bv(2) sebv(2)];
        v_r2(i,:,j)=[R2v*100 NaN];
        v_r2_rec(i,:,j)=[(1-sum(e(index_RECh).^2)/sum(e_HA(index_RECh).^2))*100 NaN];
        v_r2_exp(i,:,j)=[(1-sum(e(index_EXPh).^2)/sum(e_HA(index_EXPh).^2))*100 NaN];
        
        if runOoS==1;
            temp=OoSstudy_FamaBliss([NaN(h,1); Y],[X(:,2);NaN(h,1)],REC(firstobs:end,1),R,h);
            v_OoSr2(i,:,j)=[temp.R2OS temp.CW];
            v_OoSr2_rec(i,:,j)=[temp.R2OSrec temp.CWrec];
            v_OoSr2_exp(i,:,j)=[temp.R2OSexp temp.CWexp];
        end;
        
        X1=[X.*(RECh*ones(1,2)) X.*(1-(RECh*ones(1,2)))];
        
        [bv,sebv,R2v,R2vadj,v,F]=olsgmm(Y,X1,h,1);
        
        v_beta_rec(i,:,j)=[bv(2) sebv(2)];
        v_beta_exp(i,:,j)=[bv(4) sebv(4)];
        
    end;
    
    %make stars for significant coeffs
    v_beta_sign(:,j)=v_beta(:,1,j).*(2*(1-normcdf(abs(v_beta(:,1,j)./v_beta(:,2,j))))<0.05);
    v_beta_sign(v_beta_sign(:,j)==0,j)=NaN;
    v_beta_rec_sign(:,j)=v_beta_rec(:,1,j).*(2*(1-normcdf(abs(v_beta_rec(:,1,j)./v_beta_rec(:,2,j))))<0.05);
    v_beta_rec_sign(v_beta_rec_sign(:,j)==0,j)=NaN;
    v_beta_exp_sign(:,j)=v_beta_exp(:,1,j).*(2*(1-normcdf(abs(v_beta_exp(:,1,j)./v_beta_exp(:,2,j))))<0.05);
    v_beta_exp_sign(v_beta_exp_sign(:,j)==0,j)=NaN;
    
    results.return_beta(:,:,j)=[v_beta(:,1,j) v_beta_rec(:,1,j) v_beta_exp(:,1,j)];
    results.return_se(:,:,j)=[v_beta(:,2,j) v_beta_rec(:,2,j) v_beta_exp(:,2,j)];
    results.return_beta_sign(:,:,j)=[v_beta_sign(:,j) v_beta_rec_sign(:,j) v_beta_exp_sign(:,j)];
    
    results.return_isr2(:,:,j)=[v_r2(:,1,j) v_r2_rec(:,1,j) v_r2_exp(:,1,j)];
    
    if runOoS==1;
        %make stars for significant OoSR2s
        v_OoSr2_sign(:,j)=v_OoSr2(:,1,j).*(1-normcdf(v_OoSr2(:,2,j))<0.05);
        v_OoSr2_sign(v_OoSr2_sign(:,j)==0,j)=NaN;
        v_OoSr2_rec_sign(:,j)=v_OoSr2_rec(:,1,j).*(1-normcdf(v_OoSr2_rec(:,2,j))<0.05);
        v_OoSr2_rec_sign(v_OoSr2_rec_sign(:,j)==0,j)=NaN;
        v_OoSr2_exp_sign(:,j)=v_OoSr2_exp(:,1,j).*(1-normcdf(v_OoSr2_exp(:,2,j))<0.05);
        v_OoSr2_exp_sign(v_OoSr2_exp_sign(:,j)==0,j)=NaN;
        
        results.return_oosr2(:,:,j)=[v_OoSr2(:,1,j) v_OoSr2_rec(:,1,j) v_OoSr2_exp(:,1,j)];
        results.return_oosr2_sign(:,:,j)=[v_OoSr2_sign(:,j) v_OoSr2_rec_sign(:,j) v_OoSr2_exp_sign(:,j)];
    end;
    
end;

%%%%%%%%%%%%%EXPECTATION HYPOTHESIS REGRESSIONS%%%%%%%%%%%%%%%%%%%%%%%%%

[c_idx min_max_idx]=find(ones(2,1)*v_mat==[min_mat max_mat]'*ones(1,N+1));
v_idx=min_max_idx(1):min_max_idx(2);
N1=size(v_idx,2);

v_beta=NaN(N1,2,2);
v_beta_rec=NaN(N1,2,2);
v_beta_exp=NaN(N1,2,2);
v_beta_sign=NaN(N1,3);
v_beta_rec_sign=NaN(N1,3);
v_beta_exp_sign=NaN(N1,3);

for j=1:2;
    
    for i=1:N1;
        
        current_idx=v_idx(i);
        current_mat=v_mat(current_idx);
        
        %find first observation for current maturity
        sumnans = sum(isnan(m_y(:,current_idx)));
        cumsumnans = cumsum(isnan(m_y(:,current_idx)));
        firstobs = find((sumnans - [0; cumsumnans(1:end-1)])==0,1,'first');
        
        if j==1 %for long rate regression
            
            Y=[m_y(firstobs+h:end,current_idx-1)-m_y(firstobs:end-h,current_idx)];
            X=[ones(size(Y,1),1) h/(12*current_mat-h)*(m_y(firstobs:end-h,current_idx)-yh(firstobs:end-h,1))];
            RECh=REC(firstobs:end-h,1);
            
        elseif j==2 %for short rate regression
            
            SY=0;
            for o=1:current_mat*12/h-1;
                SY=SY+[(1-o/(current_mat*12/h))*(yh(firstobs+h*o:end,1)-yh(firstobs+h*(o-1):end-h,1)); NaN(h*o,1)];
            end;
            
            Y=SY(1:end-(current_mat*12/h-1)*h,1);
            X=[ones(size(Y,1),1) (m_y(firstobs:end-(current_mat*12/h-1)*h,current_idx)-yh(firstobs:end-(current_mat*12/h-1)*h,1))];
            RECh=REC(firstobs:end-(current_mat*12/h-1)*h,1);
            
        end
        
        if j==1
            [bv,sebv,R2v,R2vadj,v,F]=olsgmm(Y,X,h,1);
        elseif j==2
            [bv,sebv,R2v,R2vadj,v,F]=olsgmm(Y,X,(current_mat-1)*h,1);
        end
        
        v_beta(i,:,j)=[bv(2) sebv(2)];
        
        X1=[X.*(RECh*ones(1,2)) X.*(1-(RECh*ones(1,2)))];
        
        if j==1
            [bv,sebv,R2v,R2vadj,v,F]=olsgmm(Y,X1,h,1);
        elseif j==2
            [bv,sebv,R2v,R2vadj,v,F]=olsgmm(Y,X1,(current_mat-1)*h,1);
        end
        
        v_beta_rec(i,:,j)=[bv(2) sebv(2)];
        v_beta_exp(i,:,j)=[bv(4) sebv(4)];
        
    end;
    
    %make stars for significant coeffs
    v_beta_sign(:,j)=v_beta(:,1,j).*(2*(1-normcdf(abs((v_beta(:,1,j)-1)./v_beta(:,2,j))))<0.05);
    v_beta_sign(v_beta_sign(:,j)==0,j)=NaN;
    v_beta_rec_sign(:,j)=v_beta_rec(:,1,j).*(2*(1-normcdf(abs((v_beta_rec(:,1,j)-1)./v_beta_rec(:,2,j))))<0.05);
    v_beta_rec_sign(v_beta_rec_sign(:,j)==0,j)=NaN;
    v_beta_exp_sign(:,j)=v_beta_exp(:,1,j).*(2*(1-normcdf(abs((v_beta_exp(:,1,j)-1)./v_beta_exp(:,2,j))))<0.05);
    v_beta_exp_sign(v_beta_exp_sign(:,j)==0,j)=NaN;
    
    results.eht_beta(:,:,j)=[v_beta(:,1,j) v_beta_rec(:,1,j) v_beta_exp(:,1,j)];
    results.eht_se(:,:,j)=[v_beta(:,2,j) v_beta_rec(:,2,j) v_beta_exp(:,2,j)];
    results.eht_beta_sign(:,:,j)=[v_beta_sign(:,j) v_beta_rec_sign(:,j) v_beta_exp_sign(:,j)];
    
end;


end

